<?php
namespace app\client\controller;

use app\common\lib\Redis;

class MailList extends Base {
    public function __construct() {
        parent::__construct();
    }
    
    public function follows() {
        $params = request()->get();
        $pk = isset($params['lastPk']) ? intval($params['lastPk']) : 0;
        $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : 20;
        
        $where = [['follow_user_id', '=', $this->userid]];
        
        if($pk > 0) {
            $where[] = ['id', '<', $pk];
        }
        
        $resultList = [];
        
        $resultList = model('FollowUser')->where($where)->field(['user_id', 'id'])->order('id desc')->limit($pageSize)->select()->toArray();
        
        if(!empty($resultList)) {
            $userIds = [];
            foreach ($resultList as $row) {
                array_push($userIds, 'user_' . $row['user_id']);
            }
            
            $redis = new Redis();
            $userList = $redis->mget($userIds);
            $redis->close();
            
            if(!empty($userList)) {
                foreach ($resultList as $k => &$row) {
                    $row['user'] = $userList[$k];
                }

            }
        }
        
        return successResult(['list' => $resultList]);
    }
    
    public function fans() {
        $params = request()->get();
        $pk = isset($params['lastPk']) ? intval($params['lastPk']) : 0;
        $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : 20;
        
        $where = [['user_id', '=', $this->userid]];
        
        if($pk > 0) {
            $where[] = ['id', '<', $pk];
        }
        
        $resultList = [];
        
        $resultList = model('FollowUser')->where($where)->field(['follow_user_id', 'id'])->order('id desc')->limit($pageSize)->select()->toArray();
        
        if(!empty($resultList)) {
            $userIds = [];
            foreach ($resultList as $row) {
                array_push($userIds, 'user_' . $row['follow_user_id']);
            }
            
            $redis = new Redis();
            $userList = $redis->mget($userIds);
            $redis->close();
            if(!empty($userList)) {
                foreach ($resultList as $k => &$row) {
                    $row['user'] = $userList[$k];
                }
                
            }
        }
        
        return successResult(['list' => $resultList]);
    }
    
    //我的代理
    public function agent() {
        $params = request()->get();
        $pk = isset($params['lastPk']) ? intval($params['lastPk']) : 0;
        $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : 20;
        
        $where = [['merchant_id', '=', $this->userid]];
        
        if($pk > 0) {
            $where[] = ['id', '<', $pk];
        }
        
        $resultList = [];
        
        $resultList = model('Agent')->where($where)->field(['user_id', 'id'])->order('id desc')->limit($pageSize)->select()->toArray();
        
        if(!empty($resultList)) {
            $userIds = [];
            foreach ($resultList as $row) {
                array_push($userIds, 'user_' . $row['user_id']);
            }
            
            $redis = new Redis();
            $userList = $redis->mget($userIds);
            $redis->close();
            if(!empty($userList)) {
                foreach ($resultList as $k => &$row) {
                    $row['user'] = $userList[$k];
                }
                
            }
        }
        
        return successResult(['list' => $resultList]);
    }
    
    //我的商家
    public function merchant() {
        $params = request()->get();
        $pk = isset($params['lastPk']) ? intval($params['lastPk']) : 0;
        $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : 20;
        
        $where = [['user_id', '=', $this->userid]];
        
        if($pk > 0) {
            $where[] = ['id', '<', $pk];
        }
        
        $resultList = [];
        
        $resultList = model('Agent')->where($where)->field(['merchant_id', 'id'])->order('id desc')->limit($pageSize)->select()->toArray();
        
        if(!empty($resultList)) {
            $userIds = [];
            foreach ($resultList as $row) {
                array_push($userIds, 'user_' . $row['merchant_id']);
            }
            
            $redis = new Redis();
            $userList = $redis->mget($userIds);
            $redis->close();
            if(!empty($userList)) {
                foreach ($resultList as $k => &$row) {
                    $row['user'] = $userList[$k];
                }
                
            }
        }
        
        return successResult(['list' => $resultList]);
    }
}















